package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.net.payerService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.GiftDetailResp
import com.polaris.live.resp.back_resp.GiftReceivedResp
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.UserLikesVo
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.go_resp.ManagerUnMuteRequest
import com.polaris.live.resp.go_resp.OtherUserIdBean
import com.polaris.live.resp.go_resp.RemarkBean
import com.polaris.live.viewmodel.listeners.OnStreamLiveDataListener
import com.polaris.live.widget.refresh.OnListListener

/**
 * UserSpaceViewModel
 *
 * @author Created by 半仙 on 2023/9/22/022 16:01
 */
class UserSpaceViewModel : BaseViewModel(), OnListListener, OnStreamLiveDataListener {

    /**
     * 获取用户信息
     */
    var infoLiveData = MutableLiveData<ResultState<InfoOtherResp>>()
    fun getInfoUser(id: Long) {
        requestLoading({ userService.getInfoOther(id, UserManager.ALL_INFORMATION) }, infoLiveData)
    }

    /**
     * 回到页面后刷新数据
     */
    val infoFlushLiveData = MutableLiveData<ResultState<InfoOtherResp>>()
    fun flushedInfoUser(id: Long) {
        request({ userService.getInfoOther(id, UserManager.ALL_INFORMATION) }, infoFlushLiveData)
    }

    /**
     * 获取用户的直播信息
     */
    override val userLiveLiveData = MutableLiveData<DisplayUserBean>()

    /**
     * 关注
     */
    val followerLiveData = MutableLiveData<ResultState<Any>>()
    fun addFollower(otherId: Long) {
        requestLoading({ userService.addFollower(OtherUserIdBean(otherId)) }, followerLiveData)
    }

    /**
     * 点赞
     */
    var addLikeLiveData = MutableLiveData<ResultState<Any>>()
    fun addLike(otherId: Long) {
        requestLoading({ userService.addLike(OtherUserIdBean(otherId)) }, addLikeLiveData)
    }

    /**
     * 历史收到礼物记录
     */
    var giftListLiveData = MutableLiveData<ResultState<List<GiftReceivedResp>>>()
    fun getGiftReceived(userId: Long) {
        request({ payerService.getGiftReceived(userId) }, giftListLiveData)
    }

    /**
     * 礼物详情
     */
    var giftDetailLiveData = MutableLiveData<ResultState<List<GiftDetailResp>>>()
    fun getGiftDetail(giftId: Long) {
        request({ payerService.getGiftDetail(giftId) }, giftDetailLiveData)
    }

    /**
     * 添加备注
     */
    var remarkLiveData = MutableLiveData<String>()
    fun addRemark(otherId: Long, remark: String) {
        requestLoading({ userService.addRemark(RemarkBean(otherId, remark)) }, {
            remarkLiveData.value = remark
        }, { ToastGravity.showGravity(it.errorMsg) })
    }

    /**
     * 添加备注
     */
    fun putRemark(otherId: Long, remark: String) {
        requestLoading({ userService.putRemark(RemarkBean(otherId, remark)) }, {
            remarkLiveData.value = remark
        }, { ToastGravity.showGravity(it.errorMsg) })
    }

    /**
     * 喜欢我的列表
     */
    var likesLiveData = MutableLiveData<ResultState<ListResp<UserLikesVo>>>()
    private fun getLikeList(pageNumber: Int, pageSize: Int = AppConst.PAGE_SIZE) {
        request({ userService.getLikeList2(pageNumber, pageSize) }, likesLiveData)
    }


    override fun onRefresh() {
        getLikeList(pageNumber = 0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getLikeList(pageNumber = pageIndex)
    }

    /**
     * 解封
     */
    var managerUnBanLiveData = MutableLiveData<ResultState<Any>>()
    fun managerUnBan(id: Long) {
        requestLoading({ userService.managerUnBan(ManagerUnMuteRequest(id)) }, managerUnBanLiveData)
    }

}